package uno.linze.interview_question.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import uno.linze.interview_question.dto.LoginRequest;
import uno.linze.interview_question.dto.PasswordResetDto;
import uno.linze.interview_question.dto.RegisterRequest;
import uno.linze.interview_question.dto.UserInfoDTO;
import uno.linze.interview_question.service.UserService;
import uno.linze.interview_question.util.base.ApiResponse;

@RestController
@RequestMapping("/api/v1/user")
@Tag(name = "用户模块", description = "用户注册、登录及信息管理接口")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/verify-code/send")
    @Operation(summary = "发送邮箱验证码", description = "向指定邮箱发送注册验证码")
    public ApiResponse<Void> sendVerifyCode(@RequestParam String email) {
        userService.sendRegisterVerifyCode(email);
        return ApiResponse.success("验证码已发送至邮箱，有效期5天");
    }
    @PostMapping("/register")
    @Operation(summary = "用户注册", description = "新用户注册（需验证邮箱验证码）")
    public ApiResponse<UserInfoDTO> register(@Valid @RequestBody RegisterRequest request) {
        UserInfoDTO user = userService.register(request);
        return ApiResponse.success("注册成功", user);
    }
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录并获取令牌")
    public ApiResponse<String> login(@Valid @RequestBody LoginRequest request) {
        String token = userService.login(request);
        return ApiResponse.success("登录成功", token);
    }
    @GetMapping("/info")
    @Operation(summary = "查询当前用户信息", description = "获取登录用户的基本信息")
    public ApiResponse<UserInfoDTO> getUserInfo(@RequestHeader("Authorization") String token) {
        UserInfoDTO userInfo = userService.getCurrentUserInfo(token);
        return ApiResponse.success("查询成功", userInfo);
    }
    @PutMapping("/info")
    @Operation(summary = "修改用户信息", description = "更新登录用户的基本信息")
    public ApiResponse<UserInfoDTO> updateUserInfo(@RequestHeader("Authorization") String token,
                                                   @RequestBody UserInfoDTO userInfo) {
        UserInfoDTO updated = userService.updateUserInfo(userInfo,token);
        return ApiResponse.success("更新成功,重新登录", updated);
    }
    @PostMapping("/password/reset")
    @Operation(summary = "密码重置", description = "通过邮箱验证码重置密码")
    public ApiResponse<Void> resetPassword(@RequestHeader("Authorization") String token,
                                           @RequestBody PasswordResetDto passwordResetDto) {
        userService.resetPassword(passwordResetDto,token);
        return ApiResponse.success("密码重置成功");
    }

}
